<?php

namespace app\stores\controller\admin\partner;

use app\BaseController;

use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\BuyOrderModel;
use app\beautiful\model\partner\PartnerCommissionModel;

use app\beautiful\model\partner\PartnerSettingModel;
use app\beautiful\model\PartnerModel;
use app\common\model\MemberMoneyDetailModel;
use app\stores\model\order\GoodsOrderModel;
use think\Model;


class Partner extends BaseController{

    protected $name='合伙人';




    public function detail()

    {

        $partner_id = $this->request->param('partner_id/d',0);

        $role_id=$this->request->param('role_id/d',0);

        if(!in_array($role_id,[1,2,3,4,5,6])) return $this->error('参数错误');

        if(null === $partner = PartnerModel::find($partner_id)) return $this->error($this->name.'不存在');

        if($partner->shop_id != $this->request->shopId) return $this->error($this->name.'与门店不匹配');

        if($partner->is_delete == 1) return $this->error($this->name.'已删除');

        if($partner->role_id != $role_id) return $this->error($this->name.'类型不一致');
        $s=[];
        $s['partner_id']=$partner_id;
        $partner_info=PartnerModel::info($s);

        $member_ids = shopMembers($this->request->shopId);
        $customer_num=MemberModel::where([

            ['member_id','In',$member_ids],

            ['pid','=',$partner_info['member_id']]

        ])->count() ?? 0;
        $partner_info['customer_num']=$customer_num;
        $partner_info['monetary'] = PartnerCommissionModel::where([

            ['shop_id','=',$this->request->shopId],

            ['partner_id','=', $partner->partner_id]

        ])->sum('monetary');

        $partner_info['commission'] = PartnerCommissionModel::where([

            ['shop_id','=',$this->request->shopId],

            ['partner_id','=', $partner->partner_id]

        ])->sum('commission');
        $where=[];
        $where['member_id']=$partner_info['member_id'];
        $where['shop_id']=$partner_info['shop_id'];
        $where['is_paid']=1;
        $uo_info=GoodsOrderModel::user_order($where);
        $ub_info=BuyOrderModel::user_order($where);
        $partner_info['order_num']=$uo_info['all_num']+$ub_info['all_num'];
        $o_last=GoodsOrderModel::where($where)->order(['order_id'=>'desc'])->find();
        $b_last=BuyOrderModel::where($where)->order(['buy_order_id'=>'desc'])->find();
        $partner_info['last_order']='-';
        if(!empty($o_last) && empty($b_last)){
            $partner_info['last_order']=$o_last['pay_time'];
        }elseif (empty($o_last) && !empty($b_last)){
            $partner_info['last_order']=$b_last['pay_time'];
        }elseif (!empty($o_last) && !empty($b_last)){
            $pay_time=$o_last['pay_time']>$b_last['pay_time']?$o_last['pay_time']:$b_last['pay_time'];
            $partner_info['last_order']=$pay_time;
        }
        return $this->success($partner_info);

    }

    public function member_list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $partner_id = $this->request->param('partner_id',0);
        if(empty($partner_id)){
            return $this->error('partner_id参数错误');
        }
        $partner_info=PartnerModel::where(['partner_id'=>$partner_id])->find();
        if(empty($partner_info)){
            return $this->error('股东信息不存在！');
        }
        $member_ids = shopMembers($this->request->shopId);
        $where=[];
//        $where['partner_id']=$partner_info['partner_id'];
        $where['pid']=$partner_info['member_id'];
        $where['status']=1;
        $where['member_ids']=$member_ids;
        $order['a.member_id']='desc';
        $res_data=MemberModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function money_list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $partner_id = $this->request->param('partner_id',0);
        if(empty($partner_id)){
            return $this->error('partner_id参数错误');
        }
        $partner_info=PartnerModel::where(['partner_id'=>$partner_id])->find();
        if(empty($partner_info)){
            return $this->error('股东信息不存在！');
        }
        $where=[];
        $where['partner_id']=$partner_info['partner_id'];
        $where['status']=1;
        $where['types']=[4,5];
        $order['a.id']='desc';
        $res_data=MemberMoneyDetailModel::plist($where,$order,$page,$limit);
        if(!empty($res_data['list'])){
            foreach ($res_data['list'] as &$v){
                $v['start_time']=explode(' ',$v['start_time'])[0];
                $v['end_time']=explode(' ',$v['end_time'])[0];
            }
        }
        return $this->success($res_data);
    }


}

